package com.spzx.payment.service.impl;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import com.spzx.payment.configure.AlipayConfig;
import com.spzx.payment.domain.PaymentInfo;
import com.spzx.payment.service.IAlipayService;
import com.spzx.payment.service.IPaymentInfoService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Objects;

@Service
public class IAlipayServiceImpl implements IAlipayService {

    @Autowired
    private IPaymentInfoService paymentInfoService;

    @Autowired
    private AlipayClient alipayClient;


    @SneakyThrows
    @Override
    public String submitAlipay(String orderNo) {

        PaymentInfo paymentInfo = paymentInfoService.savePaymentinfo(orderNo);

        AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();
        alipayRequest.setReturnUrl(AlipayConfig.return_payment_url);
        alipayRequest.setNotifyUrl(AlipayConfig.notify_payment_url);

        HashMap<String, Object> map = new HashMap<>();
        map.put("out_trade_no",paymentInfo.getTradeNo());
        map.put("total_amount",paymentInfo.getAmount());
        map.put("subject",paymentInfo.getContent());
        map.put("product_code","QUICK_WAP_WAY");

        alipayRequest.setBizContent(JSON.toJSONString(map));

        String body = alipayClient.pageExecute(alipayRequest).getBody();
        return body;
    }
}
